package com.sunfield.windpower.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunfield.baseframe.base.MyRecyclerAdapter;
import com.sunfield.windpower.R;
import com.sunfield.windpower.bean.QuickEnterInfo;

import org.androidannotations.annotations.EBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxl on 2017/2/8.
 */
@EBean
public class QuickEnterAdapter extends MyRecyclerAdapter<QuickEnterInfo, QuickEnterAdapter.ViewHolder> {
    List<QuickEnterInfo> contactList;
    List<QuickEnterInfo> functionList;

    int selectedPosition;
    boolean isSelectedLeft;

    public QuickEnterAdapter(Context context) {
        super(context);
        contactList = new ArrayList<>();
        functionList = new ArrayList<>();
        selectedPosition = 0;
        isSelectedLeft = true;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_quick_enter, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        QuickEnterInfo left = getLeftItem(position);
        QuickEnterInfo right = getRightItem(position);

        holder.tv_name_left.setText(left.getName());
        holder.tv_name_left.setSelected(isSelected(position, true));
        holder.fl_left.setSelected(isSelected(position, true));
//        int drawableId = left.getIcon();
//        if (position == 0) {
//            drawableId = R.drawable.quick_icon1;
//        } else if (position < getType1Count()) {
//            drawableId = R.drawable.quick_icon2;
//        } else {
//            drawableId = R.drawable.quick_icon4;
//        }
        holder.fl_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select(position, true);
            }
        });

        holder.tv_name_left.setCompoundDrawablesWithIntrinsicBounds(left.getIcon(), 0, 0, 0);

        holder.fl_right.setVisibility(right == null ? View.INVISIBLE : View.VISIBLE);
        if (right == null) {
            return;
        }
        holder.tv_name_right.setText(right.getName());
        holder.tv_name_right.setSelected(isSelected(position, false));
        holder.fl_right.setSelected(isSelected(position, false));

//        if (position < getType1Count()) {
//            drawableId = R.drawable.quick_icon2;
//        } else {
//            drawableId = R.drawable.quick_icon4;
//        }
        holder.tv_name_right.setCompoundDrawablesWithIntrinsicBounds(right.getIcon(), 0, 0, 0);
        holder.fl_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select(position, false);
            }
        });
    }

    @Override
    public int getItemCount() {
        return getType1Count() + getType2Count();
    }

    public QuickEnterInfo getLeftItem(int position) {
        if (position < getType1Count()) {
            return contactList.get(position * 2);
        }
        return functionList.get((position - getType1Count()) * 2);
    }

    private void select(int position, boolean isLeft){
        int oldPositin = selectedPosition;
        selectedPosition = position;
        isSelectedLeft = isLeft;

        notifyItemChanged(position);
        if(oldPositin != position){
            notifyItemChanged(oldPositin);
        }
    }

    private boolean isSelected(int position, boolean isLeft){
        return (selectedPosition == position) && (isSelectedLeft == isLeft);
    }

    public QuickEnterInfo getRightItem(int position) {
        if (position < getType1Count()) {
            if ((position * 2 + 1) >= contactList.size())
                return null;
            return contactList.get(position * 2 + 1);
        }
        if (((position - getType1Count()) * 2 + 1) >= functionList.size()) {
            return null;
        }
        return functionList.get((position - getType1Count()) * 2 + 1);
    }

    public int getType1Count() {
        return (contactList.size() + 1) / 2;
    }

    public int getType2Count() {
        return (functionList.size() + 1) / 2;
    }

    public Context getContext() {
        return context;
    }

    public List<QuickEnterInfo> getContactList() {
        return contactList;
    }

    public List<QuickEnterInfo> getFunctionList() {
        return functionList;
    }

    public void setContactList(List<QuickEnterInfo> contactList) {
        this.contactList.clear();
        if (contactList != null) {
            this.contactList.addAll(contactList);
        }
        notifyDataSetChanged();
    }

    public void setFunctionList(List<QuickEnterInfo> functionList) {
        this.functionList.clear();
        if (functionList != null) {
            this.functionList.addAll(functionList);
        }
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_name_left;
        public TextView tv_name_right;
        public View fl_left;
        public View fl_right;

        public ViewHolder(View rootView) {
            super(rootView);
            this.tv_name_left = (TextView) rootView.findViewById(R.id.tv_name_left);
            this.tv_name_right = (TextView) rootView.findViewById(R.id.tv_name_right);
            fl_left = rootView.findViewById(R.id.fl_left);
            fl_right = rootView.findViewById(R.id.fl_right);
        }

    }
}
